package cn.yunhe.lianxi;

public class Demo {

    Person person1;
    Person person2;

    public static void main(String[] args) {
        Demo d = new Demo();
        AK47 ak47 = new AK47();
        M16 m16 = new M16();
        Wuqi wuqiP;
        Wuqi wuqiB;
        //警察随机初始枪
        int bP=(int)(Math.random()*10);
        if(bP>5){
            wuqiP=ak47;
        }else{
            wuqiP=m16;
        }
        //匪徒随机初始枪
        int bB=(int)(Math.random()*10);
        if(bB>5){
            wuqiB=ak47;
        }else{
            wuqiB=m16;
        }
        Police police = new Police("Mike", "警察", wuqiP);
        Bandit bandit = new Bandit("Tom", "匪徒", wuqiB);

        //判断谁先攻击
        int a1 = (int) (Math.random() * 10);
        if (a1 > 5) {
            d.person1 = police;
            d.person2 = bandit;
        } else {
            d.person1 = bandit;
            d.person2 = police;
        }

        police.dengChang();
        bandit.dengChang();
        //开始攻击
        while (true) {
            int a2 = (int) (Math.random() * 10);
            int a3 = (int) (Math.random() * 10);
            //一人换枪
            if (a2 > 5 && d.person1.wuqi != ak47) {
                d.person1.setWuqi(ak47);
                System.out.println(d.person1.name+"("+d.person1.sort+")" + "武器换成了" + ak47.rName());
            } else if (a2 <= 5 && d.person1.wuqi != m16) {
                d.person1.setWuqi(m16);
                System.out.println(d.person1.name+"("+d.person1.sort+")"  + "武器换成了" + m16.rName());
            }
            //另一人换枪
            if (a3 > 5 && d.person2.wuqi != ak47) {
                d.person2.setWuqi(ak47);
                System.out.println(d.person2.name+"("+d.person2.sort+")"  + "武器换成了" + ak47.rName());
            } else if (a2 <= 5 && d.person2.wuqi != m16) {
                d.person2.setWuqi(m16);
                System.out.println(d.person2.name+"("+d.person2.sort+")"  + "武器换成了" + m16.rName());
            }

            //开打
            d.person1.fight(d.person2);
            d.panDuan();
            d.person2.fight(d.person1);
            d.panDuan();
        }
    }

    //判断输赢
    public void panDuan() {
        if (person1.hp <= 0) {
            System.out.println("游戏结束" + person2.sort + "胜利");
            System.exit(0);
        } else if (person2.hp <= 0) {
            System.out.println("游戏结束" + person1.sort + "胜利");
            System.exit(0);
        }
    }
}
